package yxy.game.pm2.command.player;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playerCommand.PlayerCommandHandler;
import yxy.game.pm2.module.playerCommand.PlayerCommandModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

/**
 * @author wrs
 * @since 2022-08-04
 * @descrition 玩家点数重置
 * */
@MessageType(proto = Command.PlayerPointReset, classtype = PlayerMessage.PlayerPointResetRequest.class)
public class CMD_PlayerPointReset extends Command<PlayerMessage.PlayerPointResetRequest> {

    @Override
    public void execute(MessageSource messageSource, PlayerMessage.PlayerPointResetRequest request) throws Exception {
        String playerCode = messageSource.getTag();

        PlayerMessage.PlayerPointResetResponse.Builder response = PlayerMessage.PlayerPointResetResponse.newBuilder();

        PlayerCommandHandler handler = new PlayerCommandHandler();
        try {
            PlayerCommandModule playerCommandModule = PlayerCommandModule.get(playerCode);
            if (playerCommandModule == null){
                send(messageSource,response, Result.DataError);
            }
            //todo 重置加点
            Result result = handler.resetProperty(playerCommandModule,playerCode);

            if (!result.equals(Result.Success)){
                response.setSurplusPoint(playerCommandModule.getPoint());
                send(messageSource,response,result);
            }


            Result packResult = handler.packResetInfo(playerCommandModule,response);

            send(messageSource,response,packResult);

            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
            PlayTypeHandle.savePlayerCommand(playerCode,playTypeModule);
            new BattleHandle().powerUpdate(playerCode); //战力更新
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void send(MessageSource messageSource, PlayerMessage.PlayerPointResetResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
